package com.hp.android.logmonitor;



import com.hp.android.logmonitor.service.LogMonitor;
import com.hp.android.logmonitor.service.LogMonitorService;
import com.hp.android.logmonitor.service.LogMonitorService.LocalBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.EditText;
import android.widget.TextView;

public class LogMonitorActivity extends Activity {
	
	LogMonitorService mService;
	boolean mBound = false;
	EditText mLogTextView;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mLogTextView = (EditText)findViewById(R.id.logTextView);
		mLogTextView.setEnabled(false);
		mLogTextView.setClickable(false);
		mLogTextView.setLongClickable(false);
	}

	@Override
	protected void onStart() {
		Intent intent = new Intent(LogMonitorActivity.this,
				LogMonitorService.class);
		intent.putExtra(LogMonitor.LOG_MESSGENER, new Messenger(mHandler));
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
		super.onStart();
	}

	@Override
	protected void onStop() {
		if (mBound) {
			unbindService(mConnection);
			mBound = false;
		}
		super.onStop();
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mBound = false;
			mService = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			mService = binder.getService();
			mBound = true;
		}
	};
	
	private Handler mHandler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Bundle extras = msg.getData();
			StringBuilder logString = new StringBuilder();
			logString.append(extras.getString(LogMonitor.MSG_CALLBACK));			
			mLogTextView.append(logString.toString());
		}
	};

}